package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> data = permissionMapper.queryForResult(qo);
        return new PageInfo<>(data);
    }

    @Autowired
    private ApplicationContext ctx;//上下文路径

    /*@Override
    public void reload() {
        //查询全部权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //从spring容器中获取获取所有的controller
        Map<String, Object> annotations = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有value值
        Collection<Object> values = annotations.values();
        //遍历循环获取每一个controller
        for (Object value : values) {
            //获取controller的字节码对象
            Class<?> clazz = value.getClass();
            //获取controller的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历出每一个方法
            for (Method method : methods) {
                //判断是否有贴自定义的权限注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if(annotation!=null){
                    //如果有贴,封装成权限对象,并插入数据库
                    String name = annotation.name();
                    String expression = annotation.expression();
                    //通过注解获取权限表达式    (方式一)
                    //通过反射拼接权限表达式    (方式二)
                    //判断权限表达式是否已经存在数据库中,如果不存在,就需要插入
                    if(!permissions.contains(expression)){
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }*/

    @Override
    public void reload() {
        //查询全部权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //从spring容器中获取获取所有的controller
        Map<String, Object> annotations = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有value值
        Collection<Object> values = annotations.values();
        //遍历循环获取每一个controller
        for (Object value : values) {
            //判断实例是否是动态代理类
            if(!AopUtils.isCglibProxy(value)){
                continue;
            }
            //获取controller的字节码对象
            Class<?> clazz = value.getClass().getSuperclass();
            //获取controller的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历出每一个方法
            for (Method method : methods) {
                //判断是否有贴自定义的权限注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if(annotation!=null){
                    //如果有贴,封装成权限对象,并插入数据库
                    String name = annotation.value()[1];
                    String expression = annotation.value()[0];
                    //通过注解获取权限表达式    (方式一)
                    //通过反射拼接权限表达式    (方式二)
                    //判断权限表达式是否已经存在数据库中,如果不存在,就需要插入
                    //排重
                    if(!permissions.contains(expression)){
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmployeeId(Long id) {
        return permissionMapper.selectByEmployeeId(id);
    }
}
